package components
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class ListCombobox extends Sprite
	{
		private var data:				Vector.<String>;
		public var allItems:			Sprite;
		public var index:				int;
		public var heightSkin:			int;
		public var maxVisibleItems:		int;
		private var textFormat:			TextFormat;
		private var locationText:		Point;
		private var embedFonts:			Boolean;
		
		private var background:			Bitmap;
		private var selectSkin:			DisplayObjectContainer;

		public function ListCombobox(selSkin:DisplayObjectContainer, color:uint,  maxVisItems:int,
									 textFormatIn:TextFormat, embedFontsIn:Boolean, locationTextIn:Point){
			data = new Vector.<String>();
			
			textFormat =  textFormatIn;
			embedFonts = embedFontsIn;
			locationText = locationTextIn;
			
			selectSkin = selSkin;
			selectSkin.x = 0;
			selectSkin.y = 0;
			
			maxVisibleItems = maxVisItems;
			heightSkin = selectSkin.height;
			color = 0xFF000000 | color;
			background = new Bitmap(new BitmapData(selectSkin.width, selectSkin.height * maxVisibleItems, true, color));
			addChild(background);
			
			addEventListener(MouseEvent.MOUSE_OVER, ItemOver);
			addEventListener(MouseEvent.MOUSE_MOVE, ItemMove);
			addEventListener(MouseEvent.MOUSE_OUT, ItemOut);
			selectSkin.visible = false;
			addChild(selectSkin);
			
			allItems = new Sprite();
			addChild(allItems);
		}
		public function set Data(Items: Vector.<String>):void {
			data = Items;
			removeAllChilds(allItems);
			index = 0;
			for (var i:int = 0; i < Items.length; i++ ){
				var temp:TextField = new TextField();
				temp.embedFonts = embedFonts;
				temp.defaultTextFormat = textFormat;
				temp.text = Items[i];
				temp.x = locationText.x;
				temp.y = heightSkin * i +locationText.y;
				allItems.addChild(temp);
			}
			this.parent.dispatchEvent(new Event("updateList"));
			function removeAllChilds(container:DisplayObjectContainer):void
			{
				while (container.numChildren)
				{
					container.removeChildAt(0);
				}
			}
		}
		private function ItemOver(e:MouseEvent):void{
			selectSkin.visible = true;
		}
		private function ItemMove(e:MouseEvent):void{
			index = mouseY / heightSkin;
			if (data != null) selectSkin.y = heightSkin * Math.min(index, data.length - 1, maxVisibleItems - 1);
		}
		private function ItemOut(e:MouseEvent):void{
			selectSkin.visible = false;
		}
		public function get Data():Vector.<String>{
			return data;
		}
	}
}